{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5ac1de45",
   "metadata": {},
   "source": [
    "## 1. 本周内容\n",
    "### 1.1  优化ASR(自动语音识别)机器人\n",
    "### 1.2  连接图灵机器人¶\n",
    "### 1.3  语音合成"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5c3420c",
   "metadata": {},
   "source": [
    "## 1. 优化百度ASR接口"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a49983d",
   "metadata": {},
   "source": [
    "1. 以下为asr代码的优化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e31fe902",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-23T02:57:56.357071Z",
     "start_time": "2022-05-23T02:57:56.349309Z"
    }
   },
   "outputs": [],
   "source": [
    "import asr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "42e82adb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-16T12:00:23.702721Z",
     "start_time": "2022-05-16T12:00:18.224010Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: SpeechRecognition in d:\\anaconda\\lib\\site-packages (3.8.1)\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install SpeechRecognition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8c0e6fe7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-23T02:58:55.281726Z",
     "start_time": "2022-05-23T02:58:55.270669Z"
    }
   },
   "outputs": [],
   "source": [
    "# wingyi : asr\n",
    "API_KEY = 'tmZZvhEEdpYeYZraOvm86f5t'\n",
    "SECRET_KEY = 'HGezBzGGOz5aXzELq6pLCIqTqrahLHoT'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7955e025",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-23T02:59:17.933498Z",
     "start_time": "2022-05-23T02:59:17.665741Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.08a347d04b6b83d5a56975e42f0bacbf.2592000.1655866756.282335-26192310'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 给使用者使用\n",
    "Lo_token = asr.fetch_token(API_KEY,SECRET_KEY)\n",
    "Lo_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5086996c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-23T02:59:53.904721Z",
     "start_time": "2022-05-23T02:59:53.035750Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'{\"corpus_no\":\"7100761164348419709\",\"err_msg\":\"success.\",\"err_no\":0,\"result\":[\"你好。\"],\"sn\":\"728849800611653274791\"}\\n'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "asr.asr(Lo_token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4560dbe2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-23T03:00:17.694066Z",
     "start_time": "2022-05-23T03:00:15.522616Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['你好。']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "asr_result = eval(asr.asr(Lo_token))[\"result\"]\n",
    "asr_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "87d07c3d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-23T03:01:10.937859Z",
     "start_time": "2022-05-23T03:01:10.923897Z"
    }
   },
   "outputs": [],
   "source": [
    "from bdasr import fetch_token,asr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "2b5e39c9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-23T03:10:55.396023Z",
     "start_time": "2022-05-23T03:10:17.567349Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting pipwin\n",
      "  Downloading pipwin-0.5.2.tar.gz (7.9 kB)\n",
      "Collecting docopt\n",
      "  Downloading docopt-0.6.2.tar.gz (25 kB)\n",
      "Requirement already satisfied: requests in d:\\anaconda\\lib\\site-packages (from pipwin) (2.25.1)\n",
      "Collecting pyprind\n",
      "  Downloading PyPrind-2.11.3-py2.py3-none-any.whl (8.4 kB)\n",
      "Requirement already satisfied: six in d:\\anaconda\\lib\\site-packages (from pipwin) (1.15.0)\n",
      "Requirement already satisfied: beautifulsoup4>=4.9.0 in d:\\anaconda\\lib\\site-packages (from pipwin) (4.9.3)\n",
      "Collecting js2py\n",
      "  Downloading Js2Py-0.71-py3-none-any.whl (1.0 MB)\n",
      "Requirement already satisfied: packaging in d:\\anaconda\\lib\\site-packages (from pipwin) (20.9)\n",
      "Collecting pySmartDL>=1.3.1\n",
      "  Downloading pySmartDL-1.3.4-py3-none-any.whl (20 kB)\n",
      "Requirement already satisfied: soupsieve>1.2 in d:\\anaconda\\lib\\site-packages (from beautifulsoup4>=4.9.0->pipwin) (2.2.1)\n",
      "Collecting tzlocal>=1.2\n",
      "  Downloading tzlocal-4.2-py3-none-any.whl (19 kB)\n",
      "Collecting pyjsparser>=2.5.1\n",
      "  Downloading pyjsparser-2.7.1.tar.gz (24 kB)\n",
      "Collecting pytz-deprecation-shim\n",
      "  Downloading pytz_deprecation_shim-0.1.0.post0-py2.py3-none-any.whl (15 kB)\n",
      "Collecting backports.zoneinfo\n",
      "  Downloading backports.zoneinfo-0.2.1-cp38-cp38-win_amd64.whl (38 kB)\n",
      "Collecting tzdata\n",
      "  Downloading tzdata-2022.1-py2.py3-none-any.whl (339 kB)\n",
      "Requirement already satisfied: pyparsing>=2.0.2 in d:\\anaconda\\lib\\site-packages (from packaging->pipwin) (2.4.7)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in d:\\anaconda\\lib\\site-packages (from requests->pipwin) (2020.12.5)\n",
      "Requirement already satisfied: idna<3,>=2.5 in d:\\anaconda\\lib\\site-packages (from requests->pipwin) (2.10)\n",
      "Requirement already satisfied: urllib3<1.27,>=1.21.1 in d:\\anaconda\\lib\\site-packages (from requests->pipwin) (1.26.4)\n",
      "Requirement already satisfied: chardet<5,>=3.0.2 in d:\\anaconda\\lib\\site-packages (from requests->pipwin) (4.0.0)\n",
      "Building wheels for collected packages: pipwin, docopt, pyjsparser\n",
      "  Building wheel for pipwin (setup.py): started\n",
      "  Building wheel for pipwin (setup.py): finished with status 'done'\n",
      "  Created wheel for pipwin: filename=pipwin-0.5.2-py2.py3-none-any.whl size=8776 sha256=c542b941d669c927e465daca43a91fbd0e92f3b5903feaae25d80f6a4414c863\n",
      "  Stored in directory: c:\\users\\lenovo\\appdata\\local\\pip\\cache\\wheels\\f4\\a8\\76\\372879a40d2d4dd7de23efd867ed37112687429b8d0dda4545\n",
      "  Building wheel for docopt (setup.py): started\n",
      "  Building wheel for docopt (setup.py): finished with status 'done'\n",
      "  Created wheel for docopt: filename=docopt-0.6.2-py2.py3-none-any.whl size=13705 sha256=b6bd06700aabfcef08c87fc161b36d6cd30a0d3e1b78bd186450f3ae377b076c\n",
      "  Stored in directory: c:\\users\\lenovo\\appdata\\local\\pip\\cache\\wheels\\56\\ea\\58\\ead137b087d9e326852a851351d1debf4ada529b6ac0ec4e8c\n",
      "  Building wheel for pyjsparser (setup.py): started\n",
      "  Building wheel for pyjsparser (setup.py): finished with status 'done'\n",
      "  Created wheel for pyjsparser: filename=pyjsparser-2.7.1-py3-none-any.whl size=25998 sha256=ad362e458c0c7076adea1f287ecc62b7902abe9401051345d7723b53ca099e01\n",
      "  Stored in directory: c:\\users\\lenovo\\appdata\\local\\pip\\cache\\wheels\\d5\\88\\34\\ccb5bb40eb3178a134eb293e6c363928c5bcfba0b91031db76\n",
      "Successfully built pipwin docopt pyjsparser\n",
      "Installing collected packages: tzdata, backports.zoneinfo, pytz-deprecation-shim, tzlocal, pyjsparser, pySmartDL, pyprind, js2py, docopt, pipwin\n",
      "Successfully installed backports.zoneinfo-0.2.1 docopt-0.6.2 js2py-0.71 pipwin-0.5.2 pySmartDL-1.3.4 pyjsparser-2.7.1 pyprind-2.11.3 pytz-deprecation-shim-0.1.0.post0 tzdata-2022.1 tzlocal-4.2\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install pipwin "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "6e145807",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-23T03:18:27.176808Z",
     "start_time": "2022-05-23T03:18:27.163842Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-18-cd5d3650b53e>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-18-cd5d3650b53e>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    pipwin install PyAudio\u001b[0m\n\u001b[1;37m           ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "pipwin install PyAudio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88f7c332",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-23T03:10:55.435398Z",
     "start_time": "2022-05-23T03:10:23.390Z"
    }
   },
   "outputs": [],
   "source": [
    "# 准备音频文件\n",
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b562828",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-23T03:10:55.442388Z",
     "start_time": "2022-05-23T03:10:26.346Z"
    }
   },
   "outputs": [],
   "source": [
    "asr(token = Lo_token, AUDIO_FILE = '1.wav')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9173982b",
   "metadata": {},
   "source": [
    "## 2. 图灵机器API测试"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1f73c837",
   "metadata": {},
   "source": [
    "### 中英互译\n",
    "* 能力介绍\n",
    "\n",
    "> 中英互译是一项在线翻译服务，提供中文和英语的互译服务。可以通过使用中英互译功能为用户提供实时优质的翻译服务，提升产品体验。您只需要通过开启此功能，传入待翻译的内容，并指定要翻译的源语言和目标语言种类，就可以得到相应的翻译结果。\n",
    "\n",
    "* 使用示例\n",
    "\n",
    "> “苹果用英语怎么说”“苹果英语怎么说”“英语说苹果”\n",
    "\n",
    "* url\n",
    "\n",
    "> http://www.tuling123.com/openapi/api\n",
    "\n",
    "* 请求方法\n",
    "\n",
    "> HTTP POST\n",
    "\n",
    "* 请求参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "98523e18",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-16T12:14:09.833479Z",
     "start_time": "2022-05-16T12:14:09.826628Z"
    }
   },
   "outputs": [],
   "source": [
    "import requests,json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "069b2eac",
   "metadata": {},
   "outputs": [],
   "source": [
    "TL_Key = ''\n",
    "\n",
    "TL_url = ''\n",
    "\n",
    "headers={\n",
    "    \"Content-Type\":\"application/json\"\n",
    "}\n",
    "\n",
    "payload={\n",
    "    \n",
    "\t\"reqType\":0,\n",
    "    \"perception\": {\n",
    "        \"inputText\": {\n",
    "            \"text\": \"附近的酒店\"\n",
    "        },\n",
    "        \"inputImage\": {\n",
    "            \"url\": \"imageUrl\"\n",
    "        },\n",
    "        \"selfInfo\": {\n",
    "            \"location\": {\n",
    "                \"city\": \"北京\",\n",
    "                \"province\": \"北京\",\n",
    "                \"street\": \"信息路\"\n",
    "            }\n",
    "        }\n",
    "    },\n",
    "    \"userInfo\": {\n",
    "        \"apiKey\": \"\",\n",
    "        \"userId\": \"\"\n",
    "    }\n",
    "}\n",
    "\n",
    "payload_json = "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7b112a64",
   "metadata": {},
   "outputs": [],
   "source": [
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8d07c129",
   "metadata": {},
   "outputs": [],
   "source": [
    "res.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e79fc4a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 封装函数\n",
    "def TuLing(TL_key,text):   \n",
    "    TL_url = \"http://openapi.turingapi.com/openapi/api/v2\"\n",
    "    payload={\n",
    "        \"reqType\":0,\n",
    "        \"perception\": {\n",
    "            \"inputText\": {\n",
    "                \"text\": text\n",
    "            },\n",
    "        },\n",
    "        \"userInfo\": {\n",
    "            \"apiKey\": TL_key,\n",
    "            \"userId\": \"1234\"\n",
    "        }\n",
    "    }\n",
    "\n",
    "    res = requests.post(TL_url,data=json.dumps(payload))\n",
    "    return res.json()['results'][0]['values']['text']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2f58367",
   "metadata": {},
   "source": [
    "\n",
    "## 有图灵接口的语音识别对话机器人\n",
    "* 目标：我说一句话，电脑回复我文本信息（TULING_API)\n",
    "> 1. 準備音頻（臨時文件-時間期限）---音頻文件\n",
    "  2. 調用百度語音識別---文本信息（識別到的内容）\n",
    "  3. 調用圖靈機器人---文本信息（智能返回的内容）\n",
    "  \n",
    "> 4. （待做）。文本信息-->語音合成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1b8be3b9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d719499",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "347bcfeb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
